package cn.shenlong.portal.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.shenlong.common.Constants;
import cn.shenlong.common.Page;
import cn.shenlong.common.Pagination;
import cn.shenlong.portal.domain.Company;
import cn.shenlong.portal.domain.Icon;
import cn.shenlong.portal.service.CompanyService;
import cn.shenlong.portal.service.IconService;
import cn.shenlong.util.UploadUtil;

@Controller
@RequestMapping("/company/admin")
public class CompanyAController {
	@Autowired
	private CompanyService companyService;
	@Autowired
	private IconService iconService;
	
	@RequestMapping("/index")
	public String index(){
		return "/server/company/index";
	}
	
	@RequestMapping("/list")
	public String main(Company company,String searchProperty, String searchValue,Integer pageSize, Integer pageNumber,ModelMap model){
		if(pageSize==null){
			pageSize = 20;
		}
		if(pageNumber==null){
			pageNumber = 1;
		}
		
		Map<String,Object> parm = new HashMap<String,Object>();
		if(searchProperty!=null){
			parm.put(searchProperty, searchValue);
			model.put("searchValue", searchValue);
			parm.put("searchProperty", searchProperty);
			model.put("searchProperty", searchProperty);
		}
		Page<Company> p = companyService.findPageByParm(parm, pageNumber, pageSize);
		
		List<Company> lst = (List<Company>) p.getList();
		for (Company c : lst) {
			if (c != null && c.getIconId() != null) {
				Icon icon = this.iconService.findIconByIconId(c.getIconId());
				c.setIcon(icon);
			}
		}
		
		model.put("page", p);
		
		return "/server/company/list";
	}
	
	@RequestMapping("/addCompany")
	public String addCompany(ModelMap model){
		@SuppressWarnings("unchecked")
		List<Icon> icons = (List<Icon>) iconService.findIcon(null, null, null, null).getList();
		model.put("icons", icons);
		return "/server/company/add";
	}
	
	@RequestMapping("/showCompany")
	public String showCompany(Long companyId,ModelMap model){		
		Company company = this.companyService.findCompanyByCompanyId(companyId);
		if (company != null && company.getIconId() != null) {
			Icon icon = this.iconService.findIconByIconId(company.getIconId());
			company.setIcon(icon);
		}
		model.put("company", company);
		return "/server/company/show";
	}
	
	@RequestMapping("/saveCompany")
	public String saveCompany(Company company,ModelMap model,MultipartHttpServletRequest request){
		if(company == null || company.getCompanyId() == null){ //新增
			try {
				List<Map<String, Object>> resultList = UploadUtil.uploadImage(request, Constants.COMPANY_IMG_URI);
				if(!resultList.isEmpty()){
					company.setImgUrl(resultList.get(0).get("imagePath").toString());					
				}
				companyService.save(company);
				if(company != null) model.put("msg", "success");
				return "redirect:list.html";
			} catch (Exception e) {
				model.put("msg", "failed");
			}
			return addCompany(model);
		} else {//编辑
			Company origionCompany = companyService.findCompanyByCompanyId(company.getCompanyId());
			try {
				List<Map<String, Object>> resultList = UploadUtil.uploadImage(request, Constants.COMPANY_IMG_URI);
				if(!resultList.isEmpty()){//有上传图片
					//删除原来的图片
					UploadUtil.deleteForUrl(origionCompany.getImgUrl());
					
					//设置新的路径
					origionCompany.setImgUrl(resultList.get(0).get("imagePath").toString());
				}
				if (company.getIsForMember() == null) company.setIsForMember(false);
				company = companyService.updateCompany(company);
				if(company != null) model.put("msg", "success");
				return "redirect:list.html";
			} catch (Exception e) {
				model.put("msg", "failed");
				model.put("company", company);
			}
			return this.editCompany(company.getCompanyId(), model);
		}
		
	}
	
	@RequestMapping("/editCompany")
	public String editCompany(Long companyId,ModelMap model){
		if(model == null || model.get("company") == null){			
			Company company = this.companyService.findCompanyByCompanyId(companyId);
			model.put("company", company);
		}
		@SuppressWarnings("unchecked")
		List<Icon> icons = (List<Icon>) iconService.findIcon(null, null, null, null).getList();
		model.put("icons", icons);
		return "/server/company/edit";
	}
	
	@RequestMapping("/deleteCompany")
    @ResponseBody
	public Map<String ,Object> deleteCompany(Long companyId,String companyIdStr,HttpServletRequest request, HttpServletResponse response,ModelMap model){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			if (companyId == null) {				
				String[] tmp = companyIdStr.split(",");
				Long[] companyIds = new Long[tmp.length];
				for (int i = 0; i < tmp.length; i++) {
					companyIds[i] = Long.parseLong(tmp[i]);
				}
				companyService.deleteCompany(companyIds);
			}else{
				companyService.deleteCompany(new Long[]{companyId});
			}
			resultMap.put("result", "删除成功！");	
		}catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result","删除失败！原因："+e.toString());
		}
		return resultMap;
	}
	
	/**
	 * ajax上传图片
	 */
	@RequestMapping("/uploadImage")
    @ResponseBody
	public Map<String,Object> uploadImage(HttpServletRequest request, HttpServletResponse response, ModelMap model){
		Map<String,Object>  resultMap = new HashMap<String,Object>();
		try{
			List<Map<String, Object>> resultList = UploadUtil.uploadImage(request,Constants.COMPANY_IMG_URI);	
			resultMap.put("path", resultList.get(0).get("imagePath"));
			resultMap.put("result", true);
		}catch (Exception e) {
			e.printStackTrace();
			resultMap.put("result", false);
		}		
		return resultMap;
	}
}
